﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Win8Demo.DataModel;
using Windows.UI.Xaml.Navigation;
using Win8Demo.NetWork;
using Win8Demo.Helper;

namespace Win8Demo.ViewModel
{
    public class MainPageVM : BaseVM
    {
        #region Fields
        private ObservableCollection<HoneyEntity> _Items;
        private long _minTimestamp = 0;
        private long _maxTimestamp = 9223372036854775807;
        #endregion

        #region Properties
        public ObservableCollection<HoneyEntity> Items
        {
            get { return _Items; }
            set
            {
                base.SetProperty<ObservableCollection<HoneyEntity>>(ref _Items, value, "Items");
            }
        }
        #endregion
        

        #region Override
        public override void OnNavigatedFrom(NavigationEventArgs navigationEventArgs)
        {
            base.OnNavigatedFrom(navigationEventArgs);

        }

        public override async void OnNavigatedTo(NavigationEventArgs navigationEventArgs)
        {
            base.OnNavigatedTo(navigationEventArgs);
            NetEngine netEngine = ServiceManager.GetHoneyItemsEngine("Combine_Default", _minTimestamp, _maxTimestamp, GetHoneyItemsCallback);
            await netEngine.Request();
        }
        #endregion

        #region Method
        private void GetHoneyItemsCallback(Dictionary<string, object> resultData, ErrorType errorType)
        {
            if (errorType == ErrorType.Err_Null)
            {
                List<HoneyEntity> honeyList = resultData["HoneyList"] as List<HoneyEntity>;
                honeyList.ToObservableCollection<HoneyEntity>(_Items);
            } 
        }
        #endregion
    }
}
